package DataAcces;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class DBAcces{
	//Mensaje de Error
	private static void ShowError(Exception e,String Table){
        JOptionPane.showMessageDialog(null,"ERROR\n"+e,"\nRegistro de "+Table,0);
    }
	
	//Inserta los datos en la BD
    /*public static boolean DBDataInsert(String Table,String Query){
            try{
                String str="insert into "+Table+ "(warehouse,cliente,descripcion,peso,paquetes,fecha_entrada,barcode) values "+Query;
                DBConecction.Statement.executeUpdate(str);
                return true;
            } catch (Exception e){
                    ShowError(e, Table);
                    return false;
                }
    }*/
	
	public static boolean DBDataUpdate(String Table,String Query, String ID){
        try{
            String str="Update "+Table+" set "+Query+" where Numero = "+ID;
            DBConecction.Statement.executeUpdate(str);
            return true;
        } catch (Exception e){
                ShowError(e, Table);
                return false;
            }
    }
	
	public static List GetRegisterFiltered(String Table,String Query){
            List Result = new Vector();
		try{			
                    String str="Select * From "+Table+" where "+Query;
                    ResultSet rs = DBConecction.Statement.executeQuery(str);
                    ResultSetMetaData rsm = rs.getMetaData();
                    while(rs.next()){
                        Object[] Row = new Object[rsm.getColumnCount()];
                        for(int i = 0;i<rsm.getColumnCount();i++){
                                        Row[i] = rs.getObject(i+1);
                                }
                        Result.add(Row);
                    }
                } catch (Exception e){
                        ShowError(e, Table);
            }
            return Result;
	}
	
        public static void GetRegister(String Table,JTable table,String ColmNames){
		DefaultTableModel dfm = (DefaultTableModel) table.getModel();
                dfm.setRowCount(0);
            try{			
                String str="Select "+ColmNames+" From "+Table;
                ResultSet rs = DBConecction.Statement.executeQuery(str);
                ResultSetMetaData rsm = rs.getMetaData();
            
                while(rs.next()){
                    Object[] Row = new Object[rsm.getColumnCount()];
                    for(int i = 1;i<=rsm.getColumnCount();i++){
                                    Row[i-1] = rs.getObject(i);
                            }
                    dfm.addRow(Row);
                }
            } catch (Exception e){
                ShowError(e, Table);
            }
        
	}
	
	public static void GetRegister(String Table,String Query,JTable table, String ColmNames){
		DefaultTableModel dfm = (DefaultTableModel) table.getModel();
                dfm.setRowCount(0);
		String str="Select "+ColmNames +" From "+Table+" where "+Query;
                try{			
                    ResultSet rs = DBConecction.Statement.executeQuery(str);
                    ResultSetMetaData rsm = rs.getMetaData();
                    while(rs.next()){
                        Object[] Row = new Object[rsm.getColumnCount()];
                        for(int i = 1;i<=rsm.getColumnCount();i++){
                            Row[i-1] = rs.getObject(i);
                        }
                        dfm.addRow(Row);
                    }
                } catch (Exception e){
                        ShowError(e, Table);
            }
	}

        public static boolean ValidaRegistro(String Table,String Register,String Column,String ColmNames){
		Boolean Result = false; 
			try{	
				if(!Register.equals("")){
					String str="SELECT "+ColmNames+" FROM "+Table+" where "+Column+" = "+Register;            
                                        ResultSet rs = DBConecction.Statement.executeQuery(str);
                                        if(rs.next()){
                                            Result = true;
                                        }
                                }
			}catch (Exception e){
				ShowError(e, Table);
		    }
        return Result;
	}

        public static boolean IsNumeric(JTextField Value){
            try{
                Integer.parseInt(Value.getText());
                return true;
            }catch(Exception e){
                return false;    
            }
            
        }
}
